package com.placement.app.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.placement.app.util.Constants;

public class JobUtil {
	private static final Log logger = LogFactory.getLog(JobUtil.class);

	/**
	 * Purpose: This method returns string of Date object in MM/dd/yyyy format.
	 *
	 * @param date	:Date object
	 * @return		:String(MM/dd/yyyy) format
	 */
	public static String getDateAsString(Date date){
		String strDate = "";
//		logger.debug(">>getDateAsString>> date:"+ date);
		if(date!=null){
			SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
			strDate = sdf.format(date);
		}
		return strDate;
	}
	
	public static String getDateAsStringDateTime(Date date){
		String strDate = "";
		if(date!=null){
			try{
			SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss z");
			strDate = sdf.format(date);
			}catch(Exception e){
				logger.error(">>getDateAsStringDateTime>>",e);
			}
		}
		return strDate;
	}
	
	public boolean isNumeric(String str)
	{
		boolean temp=true;
		for (int j = 0;j < str.length();j++) {
			if (!Character.isDigit(str.charAt(j))) {
				temp=false;
				break;
			}
		}
		return temp;	
	}


	/**
	 * @author D110940/Siddharth
	 * @purposed Method to format number
	 * @param
	 * @return
	 */
	public static String formatNumber(Double d)
	{
		String returnString=null;
		if(d!= null){
			NumberFormat formatter = new DecimalFormat("#,###,###.#####");
			 returnString= formatter.format(d);
		} else if(d==null){
			returnString = "";
		}else
			returnString = d.toString();
		return returnString;

	}

	
	/**
	 * @author D110940/Siddharth
	 * @purposed Method to format number
	 * @param
	 * @return
	 */
	public static String roundUpNumber(Double d)
	{
		String returnString=null;
		if(d!= null){
			NumberFormat formatter = new DecimalFormat("#,###,###");
			 returnString= formatter.format(d);
		} else if(d==null){
			returnString = "";
		}else
			returnString = d.toString();
		return returnString;

	}


	/**
	 * Purpose: This method returns Date object from given string in MM/dd/yyyy format.
	 *
	 * @param strDate	:strDate
	 * @return			:Date object
	 */
	public static Date getDateFromString(String strDate){
		Date date = null;
//		logger.debug(">>getDateFromString>> date:"+ strDate);
		if(strDate!=null && !strDate.trim().equals("")){
			SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
			try {
				date = sdf.parse(strDate);
			} catch (ParseException e) {
				logger.error(">>getDateFromString>> ",e);
			}
		}
		return date;
	}

	/**
	 * Purpose: This method convers bigDecimal Array to Long Array
	 * 
	 * @param bigDecimal	:BigDecimal Array
	 * @return				:Long Array
	 */
	public static Long[] convertBigDecimalToLong(BigDecimal[] bigDecimal) {
		Long[] longValues = null; 
		if(bigDecimal!=null){
			longValues = new Long[bigDecimal.length];
			for(int idx=0; idx<bigDecimal.length; idx++){
				if(bigDecimal[idx]!=null)
					longValues[idx] = new Long(bigDecimal[idx].longValue());
			}
		}
		return longValues;
	}

	
	/** When an exception occurs, this code snippet will print the stack trace 
     * of the exception to a string.
     *  
     * @param t	:Throwable object
     * @return	:String
     */
    public static String getStackTrace(Throwable t)
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        t.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }

    
	/** When an exception occurs, this code snippet will print the stack trace 
     * of the exception to a string.
     *  
     * @param t	:Throwable object
     * @return	:String
     */
    public static String getExceptionMessage(Throwable t)
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        pw.write(t.getMessage());
        pw.flush();
        sw.flush();
        return sw.toString();
    }
    
    
    /**
	 * @param sqlType
	 * @return
	 */
	public String getSqlType(String sqlType)
	  {
		  String schemaName="jobs";

		  if(schemaName!=null && !schemaName.trim().equals(""))
		  {
			if(!sqlType.startsWith(schemaName))
			  sqlType=schemaName+"."+sqlType;
		  }
		  return sqlType;
	  }

    
}
